﻿using System;
using System.Collections.Generic;
using System.Text;
using PPAP.Model;
using System.Data;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class UsersDAO
    {
        public UsersDAO(){}
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Users model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "USERS");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into USERS(");
            strSql.Append("ID,FAMILYNAME,FIRSTNAME,USERNAME,PASSWORD,MOBILEPHONE,EMAIL,DEPARTMENTID,POSITION,ISEXECUTEIONSPECTIONTASK,TESTDEPARTMENT,Superior,COUNTRY,SORT,fax,Superior1,Superior2)");
            strSql.Append(" values (");
            strSql.Append(":ID,:FAMILYNAME,:FIRSTNAME,:USERNAME,:PASSWORD,:MOBILEPHONE,:EMAIL,:DEPARTMENTID,:POSITION,:ISEXECUTEIONSPECTIONTASK,:TESTDEPARTMENT,:Superior,:COUNTRY,:SORT,:fax,:Superior1,:Superior2)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":FAMILYNAME", OracleType.VarChar,150),
					new OracleParameter(":FIRSTNAME", OracleType.VarChar,150),
					new OracleParameter(":USERNAME", OracleType.VarChar,150),
					new OracleParameter(":PASSWORD", OracleType.VarChar,150),
					new OracleParameter(":MOBILEPHONE", OracleType.VarChar,50),
					new OracleParameter(":EMAIL", OracleType.VarChar,150),
					new OracleParameter(":DEPARTMENTID", OracleType.Number,4),
					new OracleParameter(":POSITION", OracleType.VarChar,50),
					new OracleParameter(":ISEXECUTEIONSPECTIONTASK", OracleType.Number,4),
                                           new OracleParameter(":TESTDEPARTMENT",OracleType.Number,4),
                                           new OracleParameter(":Superior",OracleType.VarChar,150),
                                           new OracleParameter(":COUNTRY",OracleType.VarChar,50),
                                           new OracleParameter(":SORT",OracleType.VarChar,50),
                                           new OracleParameter(":fax",OracleType.VarChar,50),
                                           new OracleParameter(":Superior1",OracleType.VarChar,150),
                                           new OracleParameter(":Superior2",OracleType.VarChar,150)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.FAMILYNAME;
            parameters[2].Value = model.FIRSTNAME;
            parameters[3].Value = model.USERNAME.ToLower();
            parameters[4].Value = model.PASSWORD;
            parameters[5].Value = model.MOBILEPHONE;
            parameters[6].Value = model.EMAIL.ToLower();
            parameters[7].Value = model.DEPARTMENTID;
            parameters[8].Value = model.POSITION;
            parameters[9].Value = model.ISEXECUTEIONSPECTIONTASK;
            parameters[10].Value = model.TESTDEPARTMENT;
            parameters[11].Value = model.Superior;
            parameters[12].Value = model.Country;
            parameters[13].Value = model.Sort;
            parameters[14].Value = model.Fax;
            parameters[15].Value = model.Superior1;
            parameters[16].Value = model.Superior2;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Users model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update USERS set ");
            strSql.Append("ID=:ID,");
            strSql.Append("FAMILYNAME=:FAMILYNAME,");
            strSql.Append("FIRSTNAME=:FIRSTNAME,");
            strSql.Append("USERNAME=:USERNAME,");
            strSql.Append("PASSWORD=:PASSWORD,");
            strSql.Append("MOBILEPHONE=:MOBILEPHONE,");
            strSql.Append("EMAIL=:EMAIL,");
            strSql.Append("DEPARTMENTID=:DEPARTMENTID,");
            strSql.Append("POSITION=:POSITION,");
            strSql.Append("ISEXECUTEIONSPECTIONTASK=:ISEXECUTEIONSPECTIONTASK,TESTDEPARTMENT=:TESTDEPARTMENT,Superior=:Superior,COUNTRY=:COUNTRY,SORT=:SORT,fax=:fax,Superior1=:Superior1,Superior2=:Superior2");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":FAMILYNAME", OracleType.VarChar,150),
					new OracleParameter(":FIRSTNAME", OracleType.VarChar,150),
					new OracleParameter(":USERNAME", OracleType.VarChar,150),
					new OracleParameter(":PASSWORD", OracleType.VarChar,150),
					new OracleParameter(":MOBILEPHONE", OracleType.VarChar,50),
					new OracleParameter(":EMAIL", OracleType.VarChar,150),
					new OracleParameter(":DEPARTMENTID", OracleType.Number,4),
					new OracleParameter(":POSITION", OracleType.VarChar,50),
					new OracleParameter(":ISEXECUTEIONSPECTIONTASK", OracleType.Number,4),
                                           new OracleParameter(":TESTDEPARTMENT",OracleType.Number,4),
                                           new OracleParameter(":Superior",OracleType.VarChar,150),
                                           new OracleParameter(":COUNTRY",OracleType.VarChar,50),
                                           new OracleParameter(":SORT",OracleType.VarChar,50),
                                           new OracleParameter(":fax",OracleType.VarChar,50),
                                           new OracleParameter(":Superior1",OracleType.VarChar,150),
                                           new OracleParameter(":Superior2",OracleType.VarChar,150)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.FAMILYNAME;
            parameters[2].Value = model.FIRSTNAME;
            parameters[3].Value = model.USERNAME.ToLower();
            parameters[4].Value = model.PASSWORD;
            parameters[5].Value = model.MOBILEPHONE;
            parameters[6].Value = model.EMAIL.ToLower();
            parameters[7].Value = model.DEPARTMENTID;
            parameters[8].Value = model.POSITION;
            parameters[9].Value = model.ISEXECUTEIONSPECTIONTASK;
            parameters[10].Value = model.TESTDEPARTMENT;
            parameters[11].Value = model.Superior;
            parameters[12].Value = model.Country;
            parameters[13].Value = model.Sort;
            parameters[14].Value = model.Fax;
            parameters[15].Value = model.Superior1;
            parameters[16].Value = model.Superior2;
           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from USERS ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Users GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,FAMILYNAME,FIRSTNAME,USERNAME,PASSWORD,MOBILEPHONE,EMAIL,DEPARTMENTID,POSITION,ISEXECUTEIONSPECTIONTASK,TESTDEPARTMENT,Superior,COUNTRY,SORT,fax,Superior1,Superior2 from USERS ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            Users model = new Users();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.FAMILYNAME = ds.Tables[0].Rows[0]["FAMILYNAME"].ToString();
                model.FIRSTNAME = ds.Tables[0].Rows[0]["FIRSTNAME"].ToString();
                model.USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                model.PASSWORD = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                model.MOBILEPHONE = ds.Tables[0].Rows[0]["MOBILEPHONE"].ToString();
                model.EMAIL = ds.Tables[0].Rows[0]["EMAIL"].ToString();
                if (ds.Tables[0].Rows[0]["DEPARTMENTID"].ToString() != "")
                {
                    model.DEPARTMENTID = int.Parse(ds.Tables[0].Rows[0]["DEPARTMENTID"].ToString());
                }
                model.POSITION = ds.Tables[0].Rows[0]["POSITION"].ToString();
                if (ds.Tables[0].Rows[0]["ISEXECUTEIONSPECTIONTASK"].ToString() != "")
                {
                    model.ISEXECUTEIONSPECTIONTASK = int.Parse(ds.Tables[0].Rows[0]["ISEXECUTEIONSPECTIONTASK"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TESTDEPARTMENT"].ToString() != "")
                {
                    model.TESTDEPARTMENT = int.Parse(ds.Tables[0].Rows[0]["TESTDEPARTMENT"].ToString());
                }
                    model.Superior = ds.Tables[0].Rows[0]["Superior"].ToString();
                    model.Superior1 = ds.Tables[0].Rows[0]["Superior1"].ToString();
                    model.Superior2 = ds.Tables[0].Rows[0]["Superior2"].ToString();
                    model.Country = ds.Tables[0].Rows[0]["COUNTRY"].ToString();
                    model.Sort = ds.Tables[0].Rows[0]["SORT"].ToString();
                    model.Fax = ds.Tables[0].Rows[0]["fax"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体 根据用户名
        /// </summary>
        public Users GetModelByUserName(string username)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,FAMILYNAME,FIRSTNAME,USERNAME,PASSWORD,MOBILEPHONE,EMAIL,DEPARTMENTID,POSITION,ISEXECUTEIONSPECTIONTASK,TESTDEPARTMENT,Superior,COUNTRY,SORT,fax from USERS ");
            strSql.Append(" where username='"+username+"'  ");
            OracleParameter[] parameters = {
};

            Users model = new Users();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.FAMILYNAME = ds.Tables[0].Rows[0]["FAMILYNAME"].ToString();
                model.FIRSTNAME = ds.Tables[0].Rows[0]["FIRSTNAME"].ToString();
                model.USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                model.PASSWORD = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                model.MOBILEPHONE = ds.Tables[0].Rows[0]["MOBILEPHONE"].ToString();
                model.EMAIL = ds.Tables[0].Rows[0]["EMAIL"].ToString();
                if (ds.Tables[0].Rows[0]["DEPARTMENTID"].ToString() != "")
                {
                    model.DEPARTMENTID = int.Parse(ds.Tables[0].Rows[0]["DEPARTMENTID"].ToString());
                }
                model.POSITION = ds.Tables[0].Rows[0]["POSITION"].ToString();
                if (ds.Tables[0].Rows[0]["ISEXECUTEIONSPECTIONTASK"].ToString() != "")
                {
                    model.ISEXECUTEIONSPECTIONTASK = int.Parse(ds.Tables[0].Rows[0]["ISEXECUTEIONSPECTIONTASK"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TESTDEPARTMENT"].ToString() != "")
                {
                    model.TESTDEPARTMENT = int.Parse(ds.Tables[0].Rows[0]["TESTDEPARTMENT"].ToString());
                }
                model.Superior = ds.Tables[0].Rows[0]["Superior"].ToString();
                model.Country = ds.Tables[0].Rows[0]["COUNTRY"].ToString();
                model.Sort = ds.Tables[0].Rows[0]["SORT"].ToString();
                model.Fax = ds.Tables[0].Rows[0]["fax"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*,rownum from(select u.ID,FAMILYNAME,FIRSTNAME,USERNAME,PASSWORD,MOBILEPHONE,EMAIL,DEPARTMENTID,POSITION,ISEXECUTEIONSPECTIONTASK,d.department,sort,fax from users u left join DEPARTMENT d on u.departmentid=d.id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append( "  )t");
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*,rownum from(select u.ID,FAMILYNAME,FIRSTNAME,USERNAME,PASSWORD,MOBILEPHONE,EMAIL,DEPARTMENTID,POSITION,ISEXECUTEIONSPECTIONTASK,d.department,sort,fax from users u left join DEPARTMENT d on u.departmentid=d.id ");
            strSql.Append(" order by username )t");
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得指定国家的TM
        /// </summary>
        /// <param name="country"></param>
        /// <returns></returns>
        public DataSet GetListForCountry(string country)
        {
            string sql = string.Format("select * from users where position='BDNT/D Manager' and country='{0}'",country);
            return DbHelperOra.Query(sql);
        }

        /// <summary>
        /// 验证邮箱在系统中是否存在  包括后台用户，供应商联系人
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckEamil(string email)
        {
            string sql = string.Format(@"select * from guserinfo where lower(userid) ='{0}'", email.ToLower());
            return DbHelperOra.Query(sql).Tables[0].Rows.Count > 0;
        }

        /// <summary>
        /// 验证管理员邮箱是否已在其他表中存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckEmailNotAdmin(string email)
        {
            string sql = string.Format(@"select t.* from (
SELECT    plantid as id, email name , email  as userid, PASSWORD  as pass, ''  as CON, 0 as ctype,id SupplierCode
FROM         contactperson where isuse=1
union all
select id,familyname,username,password,mobilephone,departmentid,0 as SupplierCode from users)t where lower(t.userid)='{0}'
", email.ToLower());
            return DbHelperOra.Query(sql).Tables[0].Rows.Count > 0;
        }

        /// <summary>
        /// 获取所有Q-VP的EMAIL
        /// </summary>
        /// <returns></returns>
        public DataTable GetVPEmail(string country)
        {
            string sql = " select EMAIL from users where position='Q-VP'";
            sql = " select EMAIL from users where position='BDNT/Q Vice President'  and country='"+country+"'";  //先中方审批
            return DbHelperOra.Query(sql).Tables[0];
        }

        /// <summary>
        /// 获取所有qe
        /// </summary>
        /// <returns></returns>
        public DataSet GetQE()
        {
            string sql = "select * from users where position='Q-engineer'";
            sql = "select * from users where position='SQE'";
            return DbHelperOra.Query(sql);
        }

        /// <summary>
        /// 判断用户是否具有admin权限
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool IsAdmin(string username)
        {
            string sql = string.Format("select * from rolesuser where rolesid=1 and innername='{0}'",username);
            return DbHelperOra.Query(sql).Tables[0].Rows.Count > 0;
        }

        /// <summary>
        /// 判断用户是否是德国人
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool IsGermany(string username)
        {
            string sql = string.Format(" select * from users where username='{0}' and country='Germany'",username);
            return DbHelperOra.Query(sql).Tables[0].Rows.Count > 0;
        }

    }
}
